use mysql::{prelude::*, PooledConn};

#[derive(Debug, PartialEq, Eq)]
pub struct Catalog {
    pub catalog_id: i64,
    pub catalog_name: String,
}

#[derive(Debug, PartialEq, Eq)]
pub struct OriginalFile {
    pub file_id: i64,
    pub aliyun_vid: String,
    pub file_alias: String,
    pub catalog_id: i64,
}

pub fn get_catalogs(parent_id: i64, conn: &mut PooledConn) -> Vec<Catalog> {
    return conn
        .exec_map(
            "select catalogId, catalogName from mrms_file_catalog where parentId = ?",
            (parent_id,),
            |(catalog_id, catalog_name)| Catalog {
                catalog_id,
                catalog_name,
            },
        )
        .expect("查询目录失败");
}

pub fn get_files(catalog_id: i64, conn: &mut PooledConn) -> Vec<OriginalFile> {
    return conn
        .exec_map(
            "select fileId, aliyunVid, fileAlias, catalogId from mrms_file_origfile where catalogId = ? and isOriginal = 1",
            (catalog_id,),
            |(file_id, aliyun_vid, file_alias, catalog_id)| OriginalFile {
                file_id,
                aliyun_vid,
                file_alias,
                catalog_id,
            },
        )
        .expect("查询文件失败");
}
